package com.itxiuyixiu.tools.utils.back;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import static com.itxiuyixiu.tools.utils.back.ReturnVo.ReturnState;

/**
 * @author 黄磊
 * @date 2020/4/21
 */
public class ReturnMessage {
    public static ReturnVo<String> getSuccessReturn(String message) {
        return new ReturnVo<>(ReturnState.SUCCESS, message);
    }

    public static <T> ReturnVo<T> getSuccessReturn(T o) {
        return new ReturnVo<>(ReturnState.SUCCESS, o);
    }

    public static ReturnVo<String> getFailureReturn(String message) {
        return new ReturnVo<>(ReturnState.FAILURE, message);
    }

    public static ReturnVo<String> getFailureReturn(BindingResult bindingResult) {
        StringBuilder stringBuilder = new StringBuilder();
        bindingResult.getAllErrors().forEach(o -> {
            FieldError error = (FieldError) o;
            stringBuilder.append(error.getField()).append(" : ").append(error.getDefaultMessage()).append("\n");
        });
        return new ReturnVo<>(ReturnState.FAILURE, stringBuilder.toString());
    }
}
